//
//  ZCSearchModel.m
//
//  Created by   on 15/10/23
//  Copyright (c) 2015 __MyCompanyName__. All rights reserved.
//

#import "ZCSearchModel.h"
#import "ZCMainModel.h"
NSString *const kZCSearchModelRemark = @"remark";
NSString *const kZCSearchModelCtime = @"ctime";
NSString *const kZCSearchModelIsurl = @"isurl";
NSString *const kZCSearchModelId = @"id";
NSString *const kZCSearchModelClassify = @"classify";
NSString *const kZCSearchModelBarcode = @"barcode";
NSString *const kZCSearchModelItems = @"items";
NSString *const kZCSearchModelBrand = @"brand";
NSString *const kZCSearchModelItemCount = @"item_count";
NSString *const kZCSearchModelName = @"name";
NSString *const kZCSearchModelUrl = @"url";
NSString *const kZCSearchModelQualified = @"qualified";


@interface ZCSearchModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation ZCSearchModel

@synthesize remark = _remark;
@synthesize ctime = _ctime;
@synthesize isurl = _isurl;
@synthesize internalZCSearchModelIdentifier = _internalZCSearchModelIdentifier;
@synthesize classify = _classify;
@synthesize barcode = _barcode;
@synthesize items = _items;
@synthesize brand = _brand;
@synthesize itemCount = _itemCount;
@synthesize name = _name;
@synthesize url = _url;
@synthesize qualified = _qualified;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.remark = [self objectOrNilForKey:kZCSearchModelRemark fromDictionary:dict];
            self.ctime = [self objectOrNilForKey:kZCSearchModelCtime fromDictionary:dict];
            self.isurl = [[self objectOrNilForKey:kZCSearchModelIsurl fromDictionary:dict] doubleValue];
            self.internalZCSearchModelIdentifier = [self objectOrNilForKey:kZCSearchModelId fromDictionary:dict];
            self.classify = [self objectOrNilForKey:kZCSearchModelClassify fromDictionary:dict];
            self.barcode = [self objectOrNilForKey:kZCSearchModelBarcode fromDictionary:dict];
        
    NSObject *receivedItems = [dict objectForKey:kZCSearchModelItems];
    NSMutableArray *parsedItems = [NSMutableArray array];
    if ([receivedItems isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedItems) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedItems addObject:[ZCMainModel modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedItems isKindOfClass:[NSDictionary class]]) {
       [parsedItems addObject:[ZCMainModel modelObjectWithDictionary:(NSDictionary *)receivedItems]];
    }

    self.items = [NSArray arrayWithArray:parsedItems];
        self.brand = [self objectOrNilForKey:kZCSearchModelBrand fromDictionary:dict];
        self.itemCount = [[self objectOrNilForKey:kZCSearchModelItemCount fromDictionary:dict] doubleValue];
        self.name = [self objectOrNilForKey:kZCSearchModelName fromDictionary:dict];
        self.url = [self objectOrNilForKey:kZCSearchModelUrl fromDictionary:dict];
        self.qualified = [[self objectOrNilForKey:kZCSearchModelQualified fromDictionary:dict]doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.remark forKey:kZCSearchModelRemark];
    [mutableDict setValue:self.ctime forKey:kZCSearchModelCtime];
    [mutableDict setValue:[NSNumber numberWithDouble:self.isurl] forKey:kZCSearchModelIsurl];
    [mutableDict setValue:self.internalZCSearchModelIdentifier forKey:kZCSearchModelId];
    [mutableDict setValue:self.classify forKey:kZCSearchModelClassify];
    [mutableDict setValue:self.barcode forKey:kZCSearchModelBarcode];
    NSMutableArray *tempArrayForItems = [NSMutableArray array];
    for (NSObject *subArrayObject in self.items) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForItems addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForItems addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForItems] forKey:kZCSearchModelItems];
    [mutableDict setValue:self.brand forKey:kZCSearchModelBrand];
    [mutableDict setValue:[NSNumber numberWithDouble:self.itemCount] forKey:kZCSearchModelItemCount];
    [mutableDict setValue:self.name forKey:kZCSearchModelName];
    [mutableDict setValue:self.url forKey:kZCSearchModelUrl];
    [mutableDict setValue:[NSNumber numberWithDouble:self.qualified] forKey:kZCSearchModelQualified];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


@end
